package in.muton.edushare;

import org.apache.cayenne.access.DataContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.Page;
import org.apache.wicket.Response;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.protocol.http.WebRequestCycle;

import com.edushare.common.orm.CayenneORMInterface;

public class EduShareRequestCycle extends WebRequestCycle {
	
	private static Log log = LogFactory.getLog(EduShareRequestCycle.class);

	public EduShareRequestCycle(WebApplication application, WebRequest request,
			Response response) 
	{
		super(application, request, response);
	}
	
	@Override
	protected void onBeginRequest() {
		super.onBeginRequest();
		log.debug("Starting Request");
	}
	
	@Override
	protected void onEndRequest() {
		super.onEndRequest();
		log.debug("Ending Request");
		DataContext dataCtx = null;
		try{
			dataCtx = ((CayenneORMInterface)((EduShareSession)getSession()).getSessionORMInterface()).getDataContext();
			log.info("Modified Objects: " + dataCtx.modifiedObjects().toString());
			log.info("New Objects: " + dataCtx.newObjects().toString());
//			dataCtx.commitChanges();
			dataCtx.rollbackChanges(); //Need to test rigorously!
			log.debug("Changes roll backed!!!");
		}catch(Exception e){
			log.error("Error Rolling back changes.", e);
//			log.debug("Rolling back the uncommited changes");
			if(dataCtx==null)
				log.debug("DataContext was null.");
		}
		log.debug("Finishing Request");
	}

}
